package com.zyj.myapplication.Activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zyj.myapplication.Fragment.MyFragment;
import com.zyj.myapplication.Model.Constant;
import com.zyj.myapplication.Model.UserModel;
import com.zyj.myapplication.R;
import com.zyj.myapplication.Service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserEditActivity extends AppCompatActivity {
    private ImageView ed_return;
    private Button btn_save;
    private EditText edit_username, edit_password, edit_passwordagain;
    private Intent intent;
    private int user_id;
    private String user_name, user_password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_useredit);
        initViews();
        initEvents();
        initData();
    }

    private void initViews() {
        ed_return = (ImageView) findViewById(R.id.ed_return);
        edit_username = (EditText) findViewById(R.id.username);
        edit_password = (EditText) findViewById(R.id.edit_pass);
        edit_passwordagain = (EditText) findViewById(R.id.edit_passagain);
        btn_save = (Button) findViewById(R.id.btn_save);
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id", 0);
        user_name = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("username", "0");

    }

    private void initEvents() {
        ed_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
               /* Intent intent = new Intent(UserEditActivity.this,LoginActivity.class);
                startActivity(intent);
                Toast.makeText(UserEditActivity.this,"退出成功！！！",Toast.LENGTH_SHORT).show();*/
            }
        });
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });

    }

    private void initData() {
        edit_username.setText(user_name);
    }

    private void save() {
        UserModel userModel = new UserModel();
        Call<Integer> call = userModel.updatePwd(user_id, edit_password.getText().toString());
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                Integer result = response.body();
                String str_psd = edit_password.getText().toString();
                String str_psdagain = edit_passwordagain.getText().toString();
                if (str_psd.equals(str_psdagain)) {
                    if (result == 0) {
                        Toast.makeText(UserEditActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(UserEditActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(UserEditActivity.this, "两次密码输入不相同", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                Toast.makeText(UserEditActivity.this, "帐号获取失败", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
